﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using NHibernate.Criterion;
using Project.Common;
using Project.Model;
using System.Linq;


namespace Web
{
	/// <summary>
	/// 功能：分页列表页面
	///       (Plantable)
	/// 作者：luky
	/// 日期：2010-12-28
	/// </summary>
    public partial class DPlantable_List : AdminPage
    {
        //创建查询器
        IList<ICriterion> queryList = new List<ICriterion>();
        // 排序字段
        Order order;
        protected string Jlyear { get; set; }
        public string jlyear = DateTime.Now.Year.ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            // 排序字段
            Jlyear = base.QueryString("jlyear");            
            if (!IsPostBack)
            {
                //this.QuerySelect();
                IQueryable<DPlantable> list1 = ManagerFactory.DPlantableManager.GetAll();
                DropDownList1.DataSource = list1;
                foreach (DPlantable item in list1)
                {
                    if (DropDownList1.Items.IndexOf(new ListItem(item.Jlyear, item.Jlyear)) == -1)
                        DropDownList1.Items.Add(new ListItem(item.Jlyear, item.Jlyear));
                }


                DropDownList1.Items.Insert(0, new ListItem("全部", ""));
                BindGrid();
            }
        }
        #region 搜索

        protected void searchButton_Click(object sender, EventArgs e)
        {
            //QuerySelect();
            BindGrid();
        }

        private void QuerySelect()
        {
            // 传入条件
            // 添加自定义条件
            if (DropDownList1.Items.FindByValue(Jlyear) != null)
            {
                DropDownList1.ClearSelection();
                DropDownList1.Items.FindByValue(Jlyear).Selected = true;
            }
            if (!string.IsNullOrEmpty(Jlyear))
                queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Plantable>(d => d.Jlyear), Jlyear));

            if (!string.IsNullOrEmpty(txt_JhType.Text))
                queryList.Add(Restrictions.Eq(PropertyNameHelper.Resolve<DPlantable>(d => d.JhType), txt_JhType.Text));

            //BindGrid();
        }

        #endregion

        private void BindGrid()
        {
            this.QuerySelect();
            AspNetPager1.RecordCount = ManagerFactory.PlantableManager.GetCount(queryList);
            IList<DPlantable> list = ManagerFactory.DPlantableManager.GetPageList(AspNetPager1, queryList, order);
            GridView1.DataSource = list;
            GridView1.DataBind();
        }

        #region gridview绑定及命令事件

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            object itemId = e.CommandArgument;
            if (e.CommandName == "DeleteIt")
            {
                #region 约束检查
                bool IsExistFK = false;
                DPlantable model = ManagerFactory.DPlantableManager.Get(int.Parse(itemId.ToString()));
                if (model == null)
                {
                    ExecJsAjax("alert('记录不存在！');", Page);
                    return;
                }
                else
                { 
                    IList<ICriterion> queryList1 = new List<ICriterion>();
                    queryList1.Add(Restrictions.Eq(PropertyNameHelper.Resolve<Plantable>(d => d.JhType), model.JhType));
                    int count = ManagerFactory.PlantableManager.GetCount(queryList1);
                    IsExistFK = count > 0;
                    
                }
                #endregion
                if (!IsExistFK)
                {
                    ManagerFactory.DPlantableManager.Delete(int.Parse(itemId.ToString()));
                    this.QuerySelect();
                }
                else
                {
                    ExecJsAjax("alert('该记录已经被使用,不能删除！');", Page);
                }
                BindGrid();
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("onmouseout", "this.className=this.className.replace(new RegExp(\"( ?|^)gridSelectedItem\\\\b\"),\"\");");
                //e.Row.Attributes.Add("onmouseover", "this.className+='" + " gridSelectedItem" + "'");
            }
        }

        #endregion

        #region 分页

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            //this.QuerySelect();
            BindGrid();
        }

        #endregion

        #region 排序

        public string SortD
        {
            get
            {
                if (ViewState["SortD"] == null)
                {
                    switch (GridView1.SortDirection)
                    {
                        case SortDirection.Ascending:
                            ViewState["SortD"] = "ASC";
                            break;
                        case SortDirection.Descending:
                            ViewState["SortD"] = "DESC";
                            break;
                        default:
                            ViewState["SortD"] = "DESC";
                            break;

                    }
                }
                return Convert.ToString(ViewState["SortD"]);
            }
            set
            {
                ViewState["SortD"] = value;
            }
        }
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortD = (SortD.ToLower() == "asc" ? "DESC" : "ASC");
            order = new Order(e.SortExpression,SortD.ToLower()=="ASC".ToLower());
        }
        protected void GridView1_Sorted(object sender, EventArgs e)
        {
            //this.QuerySelect();
            BindGrid();
        }
       
        #endregion
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("List.aspx?Jlyear=" + DropDownList1.SelectedValue);

        }

    }
}
